/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DEditPasien.java
 *
 * Created on 16 Jun 10, 16:58:53
 */
package clinsoftbeta.GUI;

import clinsoftbeta.database.Database;
import com.mysql.jdbc.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Sidiq Prihatno
 */
public class DEditPasien extends javax.swing.JDialog {

    DefaultTableModel tbModel;
    String query = "";
    private Statement st;
    private Database db;
    private ResultSet rs;
    String modeOperasi = "";

    /** Creates new form DEditPasien */
    public DEditPasien(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        txtNamaPasien.setDocument(new CumaHuruf());
        setVisible(true);
        db = new Database();
        db.koneksiDatabase();
        isikanTabel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtKodePasien = new javax.swing.JTextField();
        txtNamaPasien = new javax.swing.JTextField();
        txtTmptLahirPas = new javax.swing.JTextField();
        txtTglLahirPas = new javax.swing.JTextField();
        txtKotaPas = new javax.swing.JTextField();
        txtAlamatPas = new javax.swing.JTextField();
        txtPekerjaan = new javax.swing.JTextField();
        txtNoTelpPas = new javax.swing.JTextField();
        txtJenisKelamin = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        btnSimpan = new javax.swing.JButton();
        btnBatal = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbPasien = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel6.setLayout(new java.awt.GridBagLayout());

        jLabel4.setText("Kode Pasien");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel4, gridBagConstraints);

        jLabel5.setText("Nama Pasien");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel5, gridBagConstraints);

        jLabel6.setText("Tempat Lahir");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel6, gridBagConstraints);

        jLabel7.setText("Tanggal Lahir");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel7, gridBagConstraints);

        jLabel8.setText("Jenis Kelamin");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel8, gridBagConstraints);

        jLabel9.setText("Alamat");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel9, gridBagConstraints);

        jLabel10.setText("Kota");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel10, gridBagConstraints);

        jLabel11.setText("Pekerjaan");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel11, gridBagConstraints);

        jLabel12.setText("No. Telepon");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(jLabel12, gridBagConstraints);

        txtKodePasien.setPreferredSize(new java.awt.Dimension(250, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtKodePasien, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtNamaPasien, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtTmptLahirPas, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtTglLahirPas, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtKotaPas, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtAlamatPas, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtPekerjaan, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(9, 10, 9, 10);
        jPanel6.add(txtNoTelpPas, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(7, 10, 7, 10);
        jPanel6.add(txtJenisKelamin, gridBagConstraints);

        jPanel4.add(jPanel6);

        jPanel1.add(jPanel4, java.awt.BorderLayout.PAGE_START);

        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 20, 20));

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });
        jPanel7.add(btnSimpan);

        btnBatal.setText("Batal");
        jPanel7.add(btnBatal);

        jPanel1.add(jPanel7, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setAutoscrolls(true);

        tbPasien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Kode Pasien", "Nama", "Tempat Lahir", "Tgl. Lahir", "Jenis Kelamin", "Alamat", "Kota", "Pekerjaan", "No. Telepon"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbPasien);

        jPanel2.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        // TODO add your handling code here:

        try {
            query = "UPDATE pasien SET kode_pasien = ('" + txtKodePasien.getText() + "','"
                    + txtNamaPasien.getText() + "','" + txtTmptLahirPas.getText() + "','" + txtTglLahirPas.getText() + "','" + txtJenisKelamin + "','"
                    + txtAlamatPas.getText() + "','" + txtKotaPas.getText() + "','" + txtPekerjaan.getText() + "','" + txtNoTelpPas.getText() + "')";
            PreparedStatement presttmt = db.koneksi.prepareStatement(query);
            presttmt.executeUpdate(query);
            JOptionPane.showMessageDialog(null, "Data berhasil di Ubah");

            dispose();
            
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null,
                    "Terjadi kesalahan input data! error :" + se,
                    "Kesalahan", JOptionPane.ERROR_MESSAGE);
            dispose();
        }
}//GEN-LAST:event_btnSimpanActionPerformed
    private void isikanTabel() {
        String[] tbHeader = {"kode_pasien", "nama_pasien", "tempat_lahir", "tanggal_lahir", "jenis_kelamin", "alamat", "kota", "pekerjaan", "no_telepon"};
        tbModel = new DefaultTableModel(null, tbHeader);
//        kosongkanTabel();
        try {
            query = "Select kode_pasien, nama_pasien, tempat_lahir,tanggal_lahir,jenis_kelamin, alamat, kota, pekerjaan, no_telepon from pasien";
            st = (Statement) db.koneksi.createStatement();
            rs = st.executeQuery(query);
            while (rs.next()) {
                String[] tbData = {rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9)};
                tbModel.addRow(tbData);
            }
        } catch (SQLException se) {
            se.printStackTrace();
            //System.exit(0);
        }
        tbPasien.setModel(tbModel);
        if (tbPasien.getRowCount() > 0) {
            tbPasien.addRowSelectionInterval(0, 0);
            //      tampilkanDiText();
//            aktifText(false);
//            AktifSimpan(false);
        } else {
            //btnBaruActionPerformed(null);
        }

    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DEditPasien dialog = new DEditPasien(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBatal;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbPasien;
    private javax.swing.JTextField txtAlamatPas;
    private javax.swing.JTextField txtJenisKelamin;
    private javax.swing.JTextField txtKodePasien;
    private javax.swing.JTextField txtKotaPas;
    private javax.swing.JTextField txtNamaPasien;
    private javax.swing.JTextField txtNoTelpPas;
    private javax.swing.JTextField txtPekerjaan;
    private javax.swing.JTextField txtTglLahirPas;
    private javax.swing.JTextField txtTmptLahirPas;
    // End of variables declaration//GEN-END:variables
}
